package org.qf.web;

import com.github.pagehelper.PageInfo;
import org.qf.pojo.*;
import org.qf.service.*;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import sun.security.jca.Providers;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
public class BillController {
    @Resource
    private UserService userService;
    @Resource
    private RoleService roleService;
    @Resource
    private BillService billService;
    @Resource
    private GoodService goodService;
    @Resource
    private ProviderService provideService;
    //查询
    @RequestMapping("/showBill")
    public String showBill(Model model, @RequestParam(defaultValue = "1") int pageNum, String goodName,String providerName,String isPay) {
        //去数据库查询，把数据查询出来，再跳转页面

        PageInfo<Bill> pageInfo = billService.showUser(pageNum,goodName,providerName,isPay);
        System.out.println(pageInfo + "======================");
        //把集合数据存储起来
        model.addAttribute("pageInfo", pageInfo);
        List<Provider> list= provideService.selectAll();
        model.addAttribute("provider",list);
        List<Good> goods = goodService.selectAll();
        model.addAttribute("good",goods);
        return "billList";
    }
    @RequestMapping("/addBillHTML")
    public String addBillHTML(Model model) {
        //去数据库查询，把数据查询出来，再跳转页面
        List<Good> goods = goodService.selectAll();
        List<Provider> providers = provideService.selectAll();
        model.addAttribute("goods",goods);
        model.addAttribute("provider",providers);
        return "billAdd";
    }
    @RequestMapping("/addBill")
    public String addBill(Model model,Bill bill) {
        //去数据库查询，把数据查询出来，再跳转页面
        int i = billService.addBill(bill);
        List<Good> goods = goodService.selectAll();
        List<Provider> providers = provideService.selectAll();
        model.addAttribute("goods",goods);
        model.addAttribute("provider",providers);
        if (i>0)
        {
            model.addAttribute("result","添加成功!");
            return "redirect:/showBill";
        }
        else {
            model.addAttribute("result","添加失败!");
            return "billAdd";
        }
    }
    @RequestMapping("/EditBillHtml")
    public String EditUserHtml(HttpServletResponse response, String billcode, Model model, HttpSession session) {
        Bill bill=billService.selectByID(billcode);
        List<Good> goods = goodService.selectAll();
        List<Provider> providers = provideService.selectAll();
        model.addAttribute("goods",goods);
        model.addAttribute("provider",providers);
        model.addAttribute("Bill",bill);
        return "editBill";
    }

    @RequestMapping("/EditBill")
    public String EditUser(HttpServletResponse response,Bill bill,Model model) {
        int i=billService.EditBill(bill);
        List<Good> goods = goodService.selectAll();
        List<Provider> providers = provideService.selectAll();
        model.addAttribute("goods",goods);
        model.addAttribute("provider",providers);
        if(i>0){
            //增加成功
            return "redirect:/showBill";
        }else{
            return "editBill";
        }
    }
    //删除
    @RequestMapping("/deleteBill")
    public String deleteUser(Model model,HttpSession session,Bill bill){
        int i=billService.DeleteBill(bill);
        if (i>0)
        {
            model.addAttribute("result","删除成功!");
        }
        else {
            model.addAttribute("result","删除失败!");
        }
        return "redirect:/showBill";
    }
    //删除
    @RequestMapping("/detailBill")
    public String detailBill(Model model,HttpSession session,Bill bill){
        Bill bill1 =billService.selectByID(bill.getBillcode());
        List<Good> goods = goodService.selectAll();
        List<Provider> providers = provideService.selectAll();
        for (Good good:goods) {
            if (good.getGid().equals(bill1.getGoodid()))
            {
                bill1.setGoodid(good.getName());
            }
        }
        for (Provider provider:providers) {
            if (provider.getProid().equals(bill1.getProviderid()))
            {
                bill1.setProviderid(provider.getProname());
            }
        }

        model.addAttribute("Bill",bill1);
        return "detailBill";
    }
}
